<template>
	<uni-popup ref="popup" type="center" style="z-index: 910" :is-mask-click="false">
		<view class="package" style="width:650rpx;padding:30rpx;">
			<!-- <view class="space-between top" @click="selected=1">
				<view>正常</view>
				<uni-icons type="checkbox-filled" size="20" :color="selected==1?'#558AF1':'#999'"></uni-icons>
			</view>
			<view class="space-between top" @click="selected=2">
				<view>异常</view>
				<uni-icons type="checkbox-filled" size="20" :color="selected==2?'#558AF1':'#999'"></uni-icons>
			</view> -->
			<view class="space-between top">
				<view>状态</view>
				<switch checked @change="switchChange" style="transform:scale(0.7)" />
			</view>
			<view class="top">
				<view>备注</view>
				<textarea class="textarea" v-model="value"></textarea>
			</view>
			<view class="space-between but">
				<view @click="close()">取消</view>
				<view @click="submit()">确定</view>
			</view>
		</view>
	</uni-popup>
</template>

<script setup lang="ts">
	import { errorToast, showLoading, successToast } from '@/utils/prompt';
	import { ref } from 'vue';
	import { toPublish } from '@mqtt';
	import { getDriverID } from '@/stores/driverID';
	const admin_id = getDriverID();
	const emit = defineEmits(['submit']);
	const props = defineProps({
		orderID: { type: Number },
		state: { type: String }
	});
	const popup = ref(null)
	const selected = ref(1)
	const value = ref()
	function open() {
		popup.value.open();
	}
	function close() {
		selected.value = 1
		popup.value.close();
	}
	function submit() {
		if (!selected.value) {
			errorToast('请选择审核状态')
			return
		}
		showLoading()
		if (props.state == 'doHexiaoFuhe') {
			welfareOrder()
		} else {
			deliver()
		}
	}
	//加注单膨胀审核
	function deliver() {
		const payload = {
			id: props.orderID,
			des: value.value,
			status: selected.value
		}
		toPublish('control/doFuhe', payload, (obj : any) => {
			const { code, msg } = obj;
			if (code === 1) {
				close()
				successToast(msg)
				setTimeout(() => {
					emit('submit')
				}, 1000)
			} else {
				errorToast(msg)
			}
		});
	}
	function switchChange(e : any) {
		console.log(e.detail.value)
		selected.value = e.detail.value ? 1 : 2
	}
	//福利品审核
	function welfareOrder() {
		const payload = {
			order_id: props.orderID,
			fuhe_des: value.value,
			fuhe_status: selected.value,
			admin_id
		}
		toPublish('control/doHexiaoFuhe', payload, (obj : any) => {
			const { code, msg } = obj;
			if (code === 1) {
				close()
				successToast(msg)
				setTimeout(() => {
					emit('submit')
				}, 1000)
			} else {
				errorToast(msg)
			}
		});
	}
	defineExpose({
		open,
		close,
	});
</script>

<style scoped lang="less">
	.textarea {
		background-color: #eee;
		border-radius: 5px;
		margin-top: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		height: 150rpx;
	}

	.but {
		width: 100%;
		justify-content: space-around;
		margin: 60rpx 0px 20rpx;

		view {
			width: 230rpx;
			height: 69rpx;
			border-radius: 5px;
			text-align: center;
			line-height: 69rpx;
		}

		view:nth-of-type(1) {
			color: #468FF5;
			border: 1px solid #468FF5;
			box-sizing: border-box;
		}

		view:nth-of-type(2) {
			background: #468FF5;
			color: #FFF;
		}
	}

	.top {
		margin-top: 20rpx;
	}
</style>
<style>
	.top>>>.textarea {
		width: 100%;
	}

	.top>>>.uni-switch-input:before {
		background-color: #eee;
	}
</style>